package com.cl.blog.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.blog.common.Const;
import com.cl.blog.entity.BAdmin;
import com.cl.blog.entity.BCommunity;
import com.cl.blog.entity.BNewNotice;
import com.cl.blog.entity.BNotice;
import com.cl.blog.mapper.BCommunityMapper;
import com.cl.blog.service.BAdminService;
import com.cl.blog.service.BNewNoticeService;
import com.cl.blog.mapper.BNewNoticeMapper;
import com.cl.blog.service.BNoticeService;
import com.cl.blog.util.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author object
 * @description 针对表【b_new_notice】的数据库操作Service实现
 * @createDate 2024-04-06 16:43:21
 */
@Service
public class BNewNoticeServiceImpl extends ServiceImpl<BNewNoticeMapper, BNewNotice>
		implements BNewNoticeService {

	@Autowired
	JwtUtil jwtUtil;
	@Autowired
	BCommunityMapper bCommunityMapper;
	@Autowired
	BNoticeService bNoticeService;
	@Autowired
	BAdminService bAdminService;

	@Override
	public void addNotice(Integer userId, HttpServletRequest request) {

		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_ADMIN);


		// 写通知
		BNewNotice bNewNotice = new BNewNotice();
		bNewNotice.setTitle("重审通知");
		bNewNotice.setStatus(0);
		bNewNotice.setContent("抱歉，由于您的发表内容不符合规定，存在违禁词，所以现在需要重新审核");
		bNewNotice.setAdminId(Integer.valueOf(claim.getSubject()));
		bNewNotice.setUserId(userId);
		bNewNotice.setCreateTime(LocalDateTime.now());
		bNewNotice.setType(1);

		this.save(bNewNotice);
	}

	@Override
	public void passExamine(HttpServletRequest request, String community, Integer id, Integer userId) {
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_ADMIN);

		if ("community".equals(community)) {
			BCommunity bCommunity = bCommunityMapper.selectById(id);

			// 社区审核
			BNewNotice bNewNotice = new BNewNotice();
			// 写通知
			bNewNotice.setTitle("通知");
			bNewNotice.setStatus(0);
			bNewNotice.setContent("你的社区问答已通过-->" + bCommunity.getContent().substring(0, 20) + "...");
			bNewNotice.setAdminId(Integer.valueOf(claim.getSubject()));
			bNewNotice.setUserId(userId);
			bNewNotice.setCreateTime(LocalDateTime.now());
			bNewNotice.setType(1);

			this.save(bNewNotice);
		}

	}

	@Override
	public void noPassExamine(HttpServletRequest request, String community, Integer id, Integer userId) {
		String token = request.getHeader("token");
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_ADMIN);

		if ("community".equals(community)) {
			BCommunity bCommunity = bCommunityMapper.selectById(id);

			// 社区审核
			BNewNotice bNewNotice = new BNewNotice();
			// 写通知
			bNewNotice.setTitle("通知");
			bNewNotice.setStatus(0);
			bNewNotice.setContent("你的社区问答未通过-->" + bCommunity.getContent().substring(0, 20) + "...");
			bNewNotice.setAdminId(Integer.valueOf(claim.getSubject()));
			bNewNotice.setUserId(userId);
			bNewNotice.setCreateTime(LocalDateTime.now());
			bNewNotice.setType(1);

			this.save(bNewNotice);
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void communityExamine(HttpServletRequest request, Integer userId) {
		Claims claim = jwtUtil.getClaimByToken(request.getHeader("token"), Const.GRADE_ADMIN);
		BNewNotice bNewNotice = new BNewNotice();
		bNewNotice.setAdminId(Integer.valueOf(claim.getSubject()));
		bNewNotice.setCreateTime(LocalDateTime.now());
		bNewNotice.setUserId(userId);
		bNewNotice.setTitle("通知");
		bNewNotice.setStatus(0);
		bNewNotice.setTitle("通知");
		bNewNotice.setContent("抱歉你的问答由于违反规定被平台下架");

		this.save(bNewNotice);
	}

	@Override
	public Object selectAll(HttpServletRequest request) {
		String token = request.getHeader("token");
		System.out.println(token);
		Claims claim = jwtUtil.getClaimByToken(token, Const.GRADE_USER);
		// 获取用户通知
		List<BNewNotice> notice = this.list(new QueryWrapper<BNewNotice>()
				.eq("user_id", Integer.valueOf(claim.getSubject()))
		);
		for (BNewNotice bNewNotice : notice) {
			BAdmin bAdmin = bAdminService.getById(bNewNotice.getAdminId());
			bNewNotice.setBAdmin(bAdmin.getNickname());
		}
		// 获取全部公告
		List<BNotice> announcement = bNoticeService.list(new QueryWrapper<BNotice>()
				.isNull("userId")
				.eq("status", 0)
		);
		for (BNotice bNotice : announcement) {
			BAdmin bAdmin = bAdminService.getById(bNotice.getAdminId());
			bNotice.setAdmin(bAdmin.getNickname());
		}
		return MapUtil.builder()
				.put("announcement", announcement)
				.put("notice", notice)
				.build();
	}
}




